<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class goods_brand_uqcms extends control
{
    public function index()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '15';
        $cid = isset($_GET['cid']) ? $_GET['cid'] : '';
        $wsql = '';
        if ($cid) {
            $wsql = ' cid = ' . $cid . ' and ';
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $cquery = $this->db->query("select * from " . table('category'));
        while ($crow = $this->db->fetch_array($cquery)) {
            $category[$crow['cid']] = $crow['name'];
        }
        $result = $this->db->get_all("select * from " . table('goods_brand') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $k => $v) {
                if ($v['cid']) {
                    $v['category_cn'] = $category[$v['cid']];
                } else {
                    $v['category_cn'] = '未分类';
                }
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('goods_brand') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('goods_brand') . " where id = " . $id);
            $this->assign('row', $row);
        }
        $this->display();
    }
    public function save()
    {
        if ($_POST) {
            $name = P('name', '品牌名称不能为空');
            $data['cid'] = P('cid');
            $data['images'] = P('images');
            $data['describe'] = P('describe');
            $data['px'] = P('px', '', '0');
            $data['status'] = P('status', '', '0');
            $data['addtime'] = time();
            $id = P('id', 0);
            if ($id) {
                $data['name'] = $name;
                $query = $this->db->update(table('goods_brand'), $data, 'id = ' . $id);
            } else {
                $name_arr = explode(',', $name);
                $count = 0;
                $loop = 0;
                $this->db->autocommit();
                foreach ($name_arr as $v) {
                    $data['name'] = $v;
                    $exec = $this->db->add(table('goods_brand'), $data);
                    if ($exec) {
                        $count++;
                    }
                    $loop++;
                }
                if ($count == $loop && $count > '0') {
                    $this->db->commit();
                    $query = true;
                } else {
                    $this->db->rollback();
                    $query = false;
                }
            }
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function del()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : error_json('提交编号错误');
        $query = $this->db->delete(table('goods_brand'), "id in(" . $id . ")");
        if ($query) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
}